package sxe.core.cs511.team2_2;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.image.BufferedImage;
import java.net.URL;


/** OVERVIEW:  This class provides a graphical user interface to test the methods in the blob detector class.
 * This class was developed in order to display our work on our website and does not use any of the snBench architecure.
 *  
 * @author lisap
 *
 */

public class BlobDetectorApplet extends Applet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	BlobDetectorControls controls = null;
	BlobDetector blobDetector = null;
	BlobBag b = null;
	int count = 0;
	
	
	public class ImagePanel extends JPanel{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		public BufferedImage myImage;
		
		public ImagePanel(BufferedImage bi){
			myImage = bi;
			this.setPreferredSize(new Dimension(bi.getRaster().getWidth(),bi.getRaster().getHeight())); 
		}
		
		public ImagePanel(){
			this.setPreferredSize(new Dimension(170,120)); 
		}
		
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			Graphics2D g2 = (Graphics2D)g;
			g2.drawImage(myImage, null, 0, 0);
		}
		
	}
	
	public class BlobDetectorControls extends JPanel implements ActionListener{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		public ImagePanel imagepanel = null;
		private JButton blobButton = null;
		private JButton playButton = null;
		
		public BlobDetectorControls(BufferedImage bi){
			if (bi != null)
				imagepanel = new ImagePanel(bi);
			else
				imagepanel = new ImagePanel();
			JPanel buttonpanel = new JPanel();
			playButton = new JButton("play");
			blobButton = new JButton("show blobs");
			
			buttonpanel.add(playButton);
			buttonpanel.add(blobButton);

			this.setLayout(new BorderLayout());
			this.add(imagepanel, BorderLayout.CENTER);
			this.add(buttonpanel, BorderLayout.SOUTH);
			

			playButton.addActionListener(this);
			blobButton.addActionListener(this);
			this.repaint();
		}

		public void actionPerformed(ActionEvent ev) {
			// when the play button is pushed, display next image

			if (ev.getSource() == this.playButton){
				try{
				URL url = new URL("http://cs-people.bu.edu/lisap/CS511/wajeeha.png");
				BufferedImage bi1 = null;

				
					bi1 = ImageIO.read(url);


				

				imagepanel.myImage = bi1;
				imagepanel.repaint();

				count++;
				if (count == 1)
					b = blobDetector.detectBlobs(bi1); //called with object of interest  
				System.out.println("blogbagsize= "+ b.size());
				
				}catch(Exception e){
					System.out.println("Unable to open file " + e.getMessage());
				}

			}
			if (ev.getSource() == this.blobButton){
				// draw boxes to show the bloglocations
				if (b != null){
					BufferedImage imout = blobDetector.drawBoundingBoxes(b);
					imagepanel.myImage = imout;
					imagepanel.repaint();
				}
			}
		}

	}
	
	public BlobDetectorApplet(){
	  

	}

    /** This method sets up the graphical user interface elements */
	public void init() {
		try{
			blobDetector = new BlobDetector(1);

			//open image file
			
			URL url = new URL("http://cs-people.bu.edu/lisap/CS511/background.png");
		
			BufferedImage bi = null;

			bi = ImageIO.read(url);

			add(controls = new BlobDetectorControls(bi));
			controls.imagepanel.repaint();

			blobDetector.detectBlobs(bi); //this call will create background model and return null


	}catch(Exception e){
		System.out.println("Unable to open file " + e.getMessage());
		e.printStackTrace();
	}	
		
	}

    /** This method destroys this applet */
    public void destroy() {
        remove(controls);
    }

    /** This method starts this applet */
    public void start() {
    	controls.setEnabled(true);
    }

    
    /** This method stops this applet */
    public void stop() {
    	controls.setEnabled(false);
    }

    
    /** This method processes AWT events generated by this applet */
    public void processEvent(AWTEvent e) {
  
    }

    /** @return a String representation of this applet */
    public String getAppletInfo() {
        return "Blob Detector Applet";
    }
	

	public static void main(String args[]){
		JFrame frame = new JFrame("Blob Detector");
	    
	    frame.addWindowListener (new WindowAdapter()
	    {
	        public void windowClosing (WindowEvent event)
	    {
	        System.exit(0);
	    }
	    });
	    
	    BlobDetectorApplet applet = new BlobDetectorApplet();
		applet.init();
		applet.start();
		
		frame.add("Center", applet);
	    frame.setSize(200, 200);
	    frame.setVisible(true);
	}

}

